package com.flynn.rock.config;

import com.flynn.rock.filter.SysUsernamePasswordFilter;
import com.flynn.rock.handler.SysLoginFailureHandler;
import com.flynn.rock.handler.SysLoginSuccessHandler;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

@Configuration
public class SysAuthenticationConfigurer extends AbstractAuthenticationFilterConfigurer<HttpSecurity, SysAuthenticationConfigurer, SysUsernamePasswordFilter> {

    @Resource
    private SysLoginSuccessHandler sysLoginSuccessHandler;
    @Resource
    private SysLoginFailureHandler sysLoginFailureHandler;

    public SysAuthenticationConfigurer() {
        super(new SysUsernamePasswordFilter(), "/sys/login");
    }

    @Override
    protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingUrl) {
        return new AntPathRequestMatcher(loginProcessingUrl, "POST");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        super.getAuthenticationFilter().setAuthenticationSuccessHandler(sysLoginSuccessHandler);
        super.getAuthenticationFilter().setAuthenticationFailureHandler(sysLoginFailureHandler);
    }
}
